<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;

class troubleshooting extends Controller
{
    public function _initialize()
    {
        if (Session::get('user_name') && Session::get('pass_word')) {
            $user_info = Db::table('super_user')
                ->where([
                    'user_name' => Session::get('user_name'),
                    'password' => Session::get('pass_word'),
                ])
                ->find();
            if (!$user_info) {
                $this->error('状态失效');
            }
        } else {
            $this->error('请登录');
        }
    }
    public function index()
    {
        // ip_pv();
        $list = Db::table('help')
            ->order('id desc')
            ->where('start', 'eq', 0)
            ->select();
        return $this->fetch(null, ['list' => $list, 'DataTime' => date('Y-m-d H:i:s')]);
    }
    public function delete()
    {
        $start = Db::table('help')
            ->where("`id` = $_POST[data]")
            ->update(['start' => 1]);
        if ($start) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败了');
        }
    }
    public function edit()
    {
        $info = Db::table('help')
            ->where(['id' => $_GET['id']])
            ->find();
        return $this->fetch(null, ['DataTime' => date('Y-m-d H:i:s'), 'info' => $info]);
    }
    public function imgupload()
    {
        //判断文件上传是否出错
        if (!$_FILES || $_FILES["file"]["error"]) {
            $this->error('文件太大！或上传失败');
        } else {
            //控制上传的文件类型，大小
            if (
                $_FILES["file"]["type"] == "image/gif" ||
                $_FILES["file"]["type"] == "image/png" ||
                $_FILES["file"]["type"] == "image/jpeg" ||
                $_FILES["file"]["type"] == "image/bmp"
            ) {

                $filename = "static/images/" . date('Y-m-d H:i:s') . $_FILES["file"]["name"];
                //判断文件是否存在
                if (file_exists($filename)) {
                    $this->error('该文件已存在！');
                } else {
                    move_uploaded_file($_FILES["file"]["tmp_name"], $filename);
                    $src = sinaUpdate($filename);
                    unlink($filename);
                    $this->success('上传成功', '', ['src' => $src, 'title' => date('Y-m-d H:i:s') . $_FILES["file"]["name"]], '', []);
                }
            } else {
                $this->error('文件类型不正确！');
            }
        }
    }
    public function editsave()
    {
        #保存文章 前面修改的
        $title = input('title', null);
        $the_thumbnail = input('the_thumbnail', null);
        $id = input("id", null);
        $body = input('body', null);
        if (
            $title  &&
            $the_thumbnail  &&
            $id  &&
            $body
        ) {
            $start = Db::table('help')
                ->where([
                    "id" => $_POST["id"]
                ])
                ->update([
                    'title' => $_POST['title'],
                    'the_thumbnail' => $_POST['the_thumbnail'],
                    'id' => $_POST['id'],
                    'body' => $_POST['body'],
                    'start' => 0,
                ]);
            if ($start) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败，可能原因 您未作任何修改');
            }
        } else {
            $this->error('信息错误缺少必要参数');
        }
    }
    public function editdelete()
    {
        #保存文章 前面修改的
        $title = input('title', null);
        $the_thumbnail = input('the_thumbnail', null);
        $id = input("id", null);
        $body = input('body', null);
        if (
            $title  &&
            $the_thumbnail  &&
            $id  &&
            $body
        ) {
            $start = Db::table('help')
                ->where([
                    "id" => $_POST["id"]
                ])
                ->update([
                    'start' => 1,
                ]);
            if ($start) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败，可能原因 您未作任何修改');
            }
        } else {
            $this->error('信息错误缺少必要参数');
        }
    }
    public function deletelist()
    {
        # 已删除
        $list = Db::table('help')
            ->order('id desc')
            ->where('start', 'eq', 1)
            ->select();
        return $this->fetch(null, ['list' => $list, 'DataTime' => date('Y-m-d H:i:s')]);
    }
    public function deletelistentdelete()
    {
        #这是deletelist页面的entdelete 功能
        $start = Db::table('help')
            ->where([
                "id" => $_POST["data"]
            ])
            ->delete();
        if ($start) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败了');
        }
    }
    public function deletelistrec()
    {
        #这是deletelist页面的rec 功能
        $start = Db::table('help')
            ->where([
                "id" => $_POST["data"]
            ])
            ->update([
                'start' => 0,
            ]);
        if ($start) {
            $this->success('恢复成功');
        } else {
            $this->error('恢复失败了');
        }
    }
    public function newhelpadd()
    {
        return $this->fetch(null);
    }
    public function newhelpadd_post()
    {
        $start = Db::table('help')
            ->insert([
                'title' => $_POST['title'],
                'body' => $_POST['body'],
                'the_thumbnail' => $_POST['the_thumbnail'],
                'start' => 0
            ]);
        if ($start) {
            $this->success('发布成功');
        } else {
            $this->success('发布失败');
        }
    }
    public function newhelpadd_draft()
    {
        $start = Db::table('help')
            ->insert([
                'title' => $_POST['title'],
                'body' => $_POST['body'],
                'the_thumbnail' => $_POST['the_thumbnail'],
                'start' => 2
            ]);
        if ($start) {
            $this->success('保存草稿成功');
        } else {
            $this->success('保存草稿失败');
        }
    }
    public function read_draft()
    {
        $list = Db::table('help')
            ->order('id desc')
            ->where('start', 'eq', 2)
            ->select();
        return $this->fetch(null, ['list' => $list, 'DataTime' => date('Y-m-d H:i:s')]);
    }
    public function edit_save_draft()
    {
        $start = Db::table('help')
            ->where([
                'id' => $_POST['id']
            ])
            ->update([
                'title' => $_POST['title'],
                'body' => $_POST['body'],
                'the_thumbnail' => $_POST['the_thumbnail'],
                'start' => 2
            ]);
        if ($start) {
            $this->success('保存草稿成功');
        } else {
            $this->error('保存草稿失败,可能未作任何修改');
        }
    }
    public function get_traffic()
    {
        #获取后台控制台的 访问量
        $list = Db::table('traffic')->limit(10)->order('id desc')->field('date,number')->select();
        sort($list);
        $max = Db::table('traffic')->order('number desc')->field('number')->find();
        $this->success(['list' => $list, 'max' => $max]);
    }
}
